package com.machinecontroller.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.machinecontroller.R;
import com.machinecontroller.utils.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangxuehui on 2017/12/12.
 */

public class ImageAdapter extends PagerAdapter {
    private Context mContext;
    private List<Uri> mList;
    private List<View> mViews;
    private LayoutInflater mInflater;

    public ImageAdapter(Context context, List<Uri> list) {
        this.mContext = context;
        this.mList = list;
        this.mInflater = LayoutInflater.from(context);
        this.mViews = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return mList.size() * 1000;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (mList.size() > mViews.size()) {
            position = position % mViews.size();
        } else {
            position = position % mList.size();
        }
        container.removeView(mViews.get(position));
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        int pos = position % mList.size();
        Logger.e("aaa", pos + "===" + position + "===" + getCount() + "===" + mList.size());
        View convertView;
        if (isListEmpty()) {
            convertView = addConvertView(container, pos);
        } else {
            if (mViews.get(pos) == null) {
                convertView = addConvertView(container, pos);
            } else {
                convertView = mViews.get(pos);
            }
        }
        refreshData(container, pos, convertView);
        return convertView;
    }

    private View addConvertView(ViewGroup container, int position) {
        View convertView;
        convertView = mInflater.inflate(R.layout.item_ad, container, false);
        mViews.add(convertView);
        return mViews.get(position);
    }

    private void refreshData(ViewGroup container, int position, View convertView) {
        SimpleDraweeView mIvAd = (SimpleDraweeView) convertView.findViewById(R.id.iv_ad);
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(mList.get(position))
                .setProgressiveRenderingEnabled(true)
                .build();
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(mIvAd.getController())
                .build();
        mIvAd.setController(controller);
        mIvAd.setImageURI(mList.get(position));
        container.addView(convertView);
    }

    /**
     * 所有的view是否全部缓存
     *
     * @return
     */
    private boolean isListEmpty() {
        if (mViews != null) {
            if (mViews.size() >= mList.size()) {
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }

    private static class ViewHolder {
        SimpleDraweeView mIvAd;

        private ViewHolder(View view) {

        }
    }

}
